package com.example.searchcenter.center;

import java.util.ArrayList;
import java.util.List;
 
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.searchcenter.R;
import com.example.searchcenter.R.id;
import com.example.searchcenter.R.layout;
import com.example.searchcenter.helper.JSONParser;
 
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
 
public class DetailCenter extends Activity {
 
    TextView Name;
    TextView center_id;
    TextView des;
    String id_center;
 
    // Progress Dialog
    private ProgressDialog pDialog;
 
    // JSON parser class
    JSONParser jsonParser = new JSONParser();
 
    // single product url
    private static final String url_details = "http://tinhnguyen.edu.vn/apis/getPlaceDetails";
 
    // JSON Node names
    static final String TAG_SUCCESS = "success";
    static final String TAG_CENTERS = "center";
	static final String TAG_ID_CENTER = "cid";
	static final String TAG_NAME = "name";
	static final String TAG_THUMBNAIL = "thumbnailImageUrl";
	static final String TAG_NO = "no";
	static final String TAG_STREET = "street";
	static final String TAG_WARD = "ward";
	static final String TAG_DISTRICT = "district";
	static final String TAG_CITY = "city";
	static final String TAG_CREATEDATE = "createDate";
	static final String TAG_DESCRIPTION = "description";
	static final String TAG_PRESENTER = "presenter";
	static final String TAG_PHONE = "phone";
	static final String TAG_WEBSITE = "website";
	static final String EMAIL = "email";
	static final String TAG_TYPE = "type";
	

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_center);
 
        // getting product details from intent
        Intent i = getIntent();
 
        // getting center id (pid) from intent
        id_center = i.getStringExtra(TAG_ID_CENTER);

        // Getting complete center details in background thread
        new GetProductDetails().execute();
 
    }
 
    /**
     * Background Async Task to Get complete center details
     * */
    class GetProductDetails extends AsyncTask<String, String, String> {
 
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(DetailCenter.this);
            pDialog.setMessage("Loading product details. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
 
        /**
         * Getting product details in background thread
         * */
        protected String doInBackground(String... params) {
 
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    // Check for success tag
                    int success;
                    
                    try {
                        // Building Parameters
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("id", id_center));
 
                        // getting product details by making HTTP request
                        // Note that product details url will use GET request
                        JSONObject json = jsonParser.makeHttpRequest(
                                url_details, "GET", params);
 
                        // check your log for json response
                        Log.d("Single Center Details", json.toString());
                        
                        // json success tag
                        success = json.getInt(TAG_SUCCESS);
                        //Toast.makeText(getApplicationContext(),params.toString(), Toast.LENGTH_LONG).show();
                        if (success ==  1) {
                            // successfully received product details
                            JSONArray productObj = json
                                    .getJSONArray(TAG_CENTERS); // JSON Array
                            
                            // get first product object from JSON Array
                            JSONObject product = productObj.getJSONObject(0);
                            //Toast.makeText(getApplicationContext(),product.toString(), Toast.LENGTH_LONG).show();
                            // product with this pid found
                            // Edit Text
                            Name = (TextView) findViewById(R.id.name);
                            center_id = (TextView) findViewById(R.id.id_center);
                            des = (TextView) findViewById(R.id.des);
 
                            // display product data in textview
                            Name.setText(product.getString(TAG_NAME));
                            center_id.setText(product.getString(TAG_ID_CENTER));
                            des.setText(product.getString(TAG_DESCRIPTION));
                            
                        }else{
                            // product with pid not found
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
 
            return null;
        }
 
        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once got all details
            pDialog.dismiss();
        }
    }
}
